//
//  TMZip.m
//  Sultry
//
//  Created by zhouliangfei on 2016/11/24.
//  Copyright © 2016年 zhouliangfei. All rights reserved.
//
#include "zip.h"
#include "zlib.h"
#include "unzip.h"
#include <sys/stat.h>
#import "TMZiper.h"

@interface TMZiper ()
@property(nonatomic, copy) NSString *file;
@end
@implementation TMZiper
-(instancetype)initWithFile:(NSString*)file{
    self = [super init];
    if (self) {
        [self setFile:file];
        //
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *directory = [file stringByDeletingLastPathComponent];
        if ([manager fileExistsAtPath:directory] == NO) {
            [manager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
        }
        //
        if ([manager fileExistsAtPath:file] == NO) {
            zipFile _zipFile = zipOpen([self.file UTF8String], APPEND_STATUS_CREATE);
            if (_zipFile) {
                zipClose(_zipFile, NULL);
            }
        }
    }
    return self;
}
-(BOOL)append:(NSString*)path name:(NSString*)name{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        zipFile _zipFile = zipOpen([self.file UTF8String], APPEND_STATUS_ADDINZIP);
        if(_zipFile){
            zip_fileinfo info = {{0}};
            int ret = zipOpenNewFileInZip(_zipFile, [name UTF8String], &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, -1);
            if (ret == Z_OK) {
                FILE *fp = fopen([path UTF8String], "r");
                if (fp) {
                    int read = 0;
                    unsigned char buffer[1024] = {0};
                    do{
                        read = (int)fread(buffer, 1, 1024, fp);
                        if (read >= 0){
                            zipWriteInFileInZip(_zipFile, buffer, read);
                        }
                    }while(read > 0);
                    fclose(fp);
                }
                if (zipClose(_zipFile, NULL) == Z_OK) {
                    return YES;
                }
            }
        }
    }
    return NO;
}
-(BOOL)release:(NSString*)path{
    unzFile _unzFile = unzOpen([self.file UTF8String]);
    if(_unzFile){
        unz_global_info globalInfo = {0};
        if(unzGetGlobalInfo(_unzFile, &globalInfo) == UNZ_OK){
            int ret = unzGoToFirstFile(_unzFile);
            while(ret == UNZ_OK){
                ret = unzOpenCurrentFile(_unzFile);
                if(ret == UNZ_OK){
                    unz_file_info info = {0};
                    ret = unzGetCurrentFileInfo(_unzFile, &info, NULL, 0, NULL, 0, NULL, 0);
                    if(ret == UNZ_OK){
                        char *name = malloc(info.size_filename + 1);
                        unzGetCurrentFileInfo(_unzFile, &info, name, info.size_filename + 1, NULL, 0, NULL, 0);
                        name[info.size_filename] = '\0';
                        //
                        char val = name[info.size_filename - 1];
                        BOOL dir = (val=='/' || val == '\\');
                        //
                        NSString *fileName = [NSString stringWithUTF8String:name];
                        NSString *filePath = [path stringByAppendingPathComponent:fileName];
                        if ([filePath containsString:@"\\"]) {
                            filePath = [filePath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                        }
                        NSString *directory = filePath;
                        if (dir == NO) {
                            directory = [filePath stringByDeletingLastPathComponent];
                        }
                        if ([[NSFileManager defaultManager] fileExistsAtPath:directory] == NO) {
                            [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
                        }
                        //
                        if (dir == NO) {
                            FILE *fp = fopen([filePath UTF8String], "wb");
                            if (fp) {
                                int read = 0;
                                unsigned char buffer[1024] = {0};
                                do{
                                    read = unzReadCurrentFile(_unzFile, buffer, 1024);
                                    if (read >= 0){
                                        fwrite(buffer, 1, read, fp);
                                    }
                                }while(read > 0);
                                fclose(fp);
                            }
                        }
                        free(name);
                    }
                    ret = unzCloseCurrentFile(_unzFile);
                }
                if (ret == UNZ_OK) {
                    ret = unzGoToNextFile(_unzFile);
                }
            }
        }
        if (unzClose(_unzFile) == UNZ_OK) {
            return YES;
        }
    }
    return NO;
}
@end
